<template>
  <el-card style="height: 99%;padding: 0px">
    <p style="margin-left: 10px">首页>学生信息管理</p>
    <el-button type="primary" @click="dialogFormVisible=true">添加学生</el-button>
    <el-table
        :data="tableData"
        border
        style="width: 100%;line-height: 50px;margin-top: 10px;margin-bottom: 10px">
      <el-table-column
          prop="name"
          label="学生姓名"
      >
      </el-table-column>
      <el-table-column
          prop="account"
          label="账号">
      </el-table-column>
      <el-table-column
          prop="email"
          label="邮箱">
      </el-table-column>
      <el-table-column
          prop="hometown"
          label="籍贯">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="注册时间">
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="toUpdateStudent(scope.row)">编辑学生信息</el-button>
          <el-button
              size="mini"
              @click="deleteStudent(scope.row.id)" type="danger">删除学生信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        :total="pageInfo.total"
        :page-size="pageInfo.pageSize"
        @current-change="handleCurrentChange">
    </el-pagination>
    <el-dialog title="添加学生信息" :visible.sync="dialogFormVisible" >
      <el-form v-model="addForm">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="addForm.name" required="required"></el-input>
        </el-form-item>
        <el-form-item label="学生账号" prop="account">
          <el-input v-model="addForm.account"></el-input>
        </el-form-item>
        <el-form-item label="学生密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="学生邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="学生籍贯" prop="hometown" >
          <el-input v-model="addForm.hometown"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStudent()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="更新学生信息" :visible.sync="dialogupdateFormVisible" >
      <el-form v-model="updateForm">
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="updateForm.name"></el-input>
        </el-form-item>
        <el-form-item label="学生账号" prop="account">
          <el-input v-model="updateForm.account"></el-input>
        </el-form-item>
        <el-form-item label="学生邮箱" prop="email">
          <el-input v-model="updateForm.email"></el-input>
        </el-form-item>
        <el-form-item label="学生籍贯"  prop="hometown">
          <el-input v-model="updateForm.hometown"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogupdateFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateMenu()">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {doAddStudent, doDeleteStudent, doGetStudentByPage, doUpdateStduent} from "@/api/student";
export default {
  name: "studentInfo",
  data() {
    return {
      tableData:[
      ],
      pageInfo:{
        total:1,
        pageNum:1,
        pageSize:6
      },
      addForm:{name:"",account:"",password:"",hometown:"",email:""},
      updateForm:{id:"",name:"",account:"",password:"",hometown:"",email:""},
      dialogFormVisible:false,
      dialogupdateFormVisible:false
    }
  },methods: {
      getStudent(){
          doGetStudentByPage(this.pageInfo.pageNum,this.pageInfo.pageSize).then(res=>{
              if (res.code===200){
                  this.pageInfo.total=res.data.total;
                  this.tableData=res.data.records;
              }
          })
      },
    handleCurrentChange(val) {
      this.pageInfo.pageNum=val;
      this.getStudent();
    },
    addStudent(){

        doAddStudent(this.addForm).then(res=>{
          if (res.code===200){
            this.getStudent()
            this.addForm={};
            this.dialogupdateFormVisible=false;
          }
        })

    },
    toUpdateStudent(student){
      this.updateForm=student;
      this.dialogupdateFormVisible=true;
    },
    updateMenu(){

        doUpdateStduent(this.updateForm).then(res=>{
          if (res.code===200){
            this.dialogupdateFormVisible=false;
            this.getStudent();
          }
        })
    },
    deleteStudent(id){
      doDeleteStudent(id).then(res=>{
        if (res.code===200){
          this.getStudent();
        }
      })
    }
  },
  mounted() {
    this.getStudent();
  }
}
</script>

<style lang="less" scoped>

</style>